{
  "cells": [
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "# 线性回归"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "import numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt",
      "execution_count": 1,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "## 模拟数据集"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x = np.arange(0, 10, 0.05)",
      "execution_count": 2,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "y = x + 0.5 + np.random.normal(0.5, 0.5, size=x.shape)",
      "execution_count": 3,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "plt.scatter(x, y)\nplt.show()",
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAIABJREFUeJzt3X+MnPV9J/D3Z8cPeNYIjxG+Kx4wdqXIbgjBW/Z6vlqXi6HFuTrAiqR1I6i4tBKq1PaCD7lZelFtUE7syW3h/qgqIZqGExZdwMh14tzZUewoV1ST7GbXIg5GaUOwGdOyFZ60wQOe9X7uj5ln/cwz3+/zfGeeZ3498379493ZmWeeFclnvvv5fj6fr6gqiIho8I30+gaIiCgdDOhERBnBgE5ElBEM6EREGcGATkSUEQzoREQZwYBORJQRDOhERBnBgE5ElBEr4p4gIl8B8GkA76rqx+qP7QdwN4BLAP4BwOdVtRx3reuvv143bNiQ6IaJiIbN7OzsP6vq2rjnSVzrv4h8AsDPAPzvQEC/C8BxVV0Ukf8JAKr6xbg3Gx8f15mZG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XAQwMOq+i+9vp9OEpFPA3hXVWd7fS9dtALALwL4C1UdA/A+Uv4TvN/Uc8b3AtgIYB2AVSLyQG/vKjsGIaC/DeCmwPc3IqN/ogWJiIdaMD+gqi/3+n66YBuAe0TkJ6il1e4Qked6e0sd9zaAt1XV/+vrJdQCfJb9CoA3VXVBVasAXgbwyz2+p276JxG5AQDq/76b5sUHIaB/D8BHRGSjiFyF2gbK4R7fU0eJiKCWV31dVf+s1/fTDar6qKreqKobUPtvfFxVM71yU9V/BHBORPwTy+8E8MMe3lI3nAWwVURG6/87vxMZ3wgOOQzgwfrXDwL4mzQv3veHRKvqooj8PoCjqO2If0VVT/f4tjptG4DfAvCaiMzXH/sjVf1GD++JOuMPAByoL1Z+DODzPb6fjlLVV0XkJQDfR62aaw4Z7RgVkecBfBLA9SLyNoC9AKYAvCAiv4Pah9uvp/qe7BQlIsqGQUi5EBGRAwZ0IqKMYEAnIsoIBnQiooxgQCciyggGdCKijGBAJyLKCAZ0IqKM+P8lXmP3Nk2hzwAAAABJRU5ErkJggg==\n",
            "text/plain": "<matplotlib.figure.Figure at 0x7f6511c82128>"
          },
          "metadata": {}
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "## 构建模型"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 生成一个1维的W矩阵，取值范围再[-1, 1]的随机数\nW = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')",
      "execution_count": 5,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 生成一个1维的b矩阵，初始值为0\nb = tf.Variable(tf.zeros([1]), name='b')",
      "execution_count": 6,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 构建预估y_\ny_ = W * x + b",
      "execution_count": 7,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 以预估值y_和y之间的均方差作为损失\nloss = tf.reduce_mean(tf.square(y_ - y), name='loss')\n# 优化器\nop = tf.train.GradientDescentOptimizer(0.01)\n# 训练,最小化误差值\ntrain = op.minimize(loss, name='train')",
      "execution_count": 8,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 建立会话\nsess = tf.Session()",
      "execution_count": 9,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "# 初始化变量\ninit = tf.global_variables_initializer()\nsess.run(init)\n\nprint('W=',sess.run(W),',b=',sess.run(b),',loss=',sess.run(loss))",
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": "W= [0.2702682] ,b= [0.] ,loss= 25.974218\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "for step in range(100):\n    sess.run(train)\nprint('W=',sess.run(W),',b=',sess.run(b),',loss=',sess.run(loss))",
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": "W= [1.0771836] ,b= [0.47937557] ,loss= 0.2581771\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "name": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.4",
      "file_extension": ".py",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}